package com.digitalart.site.section.subsectiontype.subsectionnav {
	import com.digitalart.site.commons.CustomEvent;
	import com.digitalart.site.commons.CustomXmlLoader;

	import flash.events.Event;
	import flash.events.EventDispatcher;

	/**
	 * @author johan.chaves
	 */
	public class NavModel extends EventDispatcher {
		public static const PATH : String = "xml/sections.xml";
		public static const ON_LOAD_NAV_DATA_READY : String = "onloadnavdataready";
		
		private var _navItems : Array;	
		
		private var _sectionId : String;				
				
		private var _data : XML;
		
		public function NavModel() {					
			super(this);			
		}
		
		public function init(id : String) : void{
			
			navItems = new Array();	
			_sectionId = id;		
			var loader : CustomXmlLoader = CustomXmlLoader.getInstance();
			loader.addEventListener(CustomXmlLoader.XML_LOAD_COMPLETE, dataLoaded);
			loader.loadXml(PATH);
		}
		
		private function dataLoaded(e:CustomEvent) : void {
			
			e.target.removeEventListener(CustomXmlLoader.XML_LOAD_COMPLETE, dataLoaded);
			_data = e.data;
			createMenuItems();
		}	

		private function createMenuItems() : void {
						
			createItems();
			dispatchEvent(new Event(ON_LOAD_NAV_DATA_READY));
		}

		private function createItems() : void {

			var itemVO : NavItemVO;
			
			for each(var itemData : XML in _data.section.(@id == _sectionId).subSectionNav.item) {				
				itemVO = new NavItemVO();
				itemVO.id = itemData.@subsectionId;
				itemVO.text = itemData;							
				navItems.push(itemVO);
			}
		}

		public function get navItems() : Array {
			return _navItems;
		}
		
		public function set navItems(menuItems : Array) : void {
			_navItems = menuItems;
		}
	}
}
